<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>数据表格</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">

    <link rel="icon" href="../img/web/web.ico"/>
    <link rel="stylesheet" href="../layui/css/layui.css"/>
    <link rel="stylesheet" href="../css/app.css"/>
<body ontouchstart="">

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">工具栏表格</div>
                <div class="layui-card-body">

                    <!-- 表格工具栏 -->
                    <div class="layui-row vip-btn-tool-box">
                        <div class="layui-col-sm12 layui-col-sm4 layui-col-md4 layui-btn-container">
                            <button class="layui-btn layui-btn-radius layui-btn-primary" id="vip-btn-add" title="新增"><i class="vip-icon">&#xe65c;</i></button>
                            <button class="layui-btn layui-btn-radius layui-btn-primary" id="vip-btn-del" title="删除"><i class="vip-icon">&#xe664;</i></button>
                            <button class="layui-btn layui-btn-radius layui-btn-primary" id="vip-btn-refresh" title="刷新"><i class="vip-icon">&#xe68a;</i></button>
                        </div>
                        <div class="layui-col-sm12 layui-col-sm8 layui-col-md8">
                            <form class="layui-form layui-form-pane">
                                <div class="layui-form-item">
                                    <label class="layui-form-label layui-label-sm">标题</label>
                                    <div class="layui-input-inline layui-input-sm">
                                        <input type="text" name="search" required="" lay-verify="required" lay-vertype="tips" placeholder="请输入内容" autocomplete="off" class="layui-input" data-form-pw="1515934826270.1904">
                                    </div>
                                    <label class="layui-form-label layui-label-sm">选择</label>
                                    <div class="layui-input-inline layui-input-sm">
                                        <select name="select">
                                            <option value=""></option>
                                            <option value="1" selected="">选择一</option>
                                            <option value="2">选择二</option>
                                            <option value="3">选择三</option>
                                            <option value="4">选择四</option>
                                        </select>
                                    </div>
                                    <button id="vip-btn-search" type="button" class="layui-btn layui-btn-radius layui-btn-primary" title="搜索"><i class="vip-icon">&#xe6b0;</i></button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div id="demo-table-2" lay-filter="table"></div>
                </div>
            </div>
        </div>

    </div>
</div>

<script type="text/html" id="menu">
    <button class="layui-btn layui-btn-xs layui-bg-cyan" lay-event="view">查看</button>
    <button class="layui-btn layui-btn-xs layui-bg-gray" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-xs layui-bg-red" lay-event="del">删除</button>
</script>

<!-- 表格工具栏 TPL -->
<!--<script type="text/html" id="tableToolTpl">
    <div class="layui-row vip-btn-tool-box">
        <div class="layui-col-sm12 layui-col-sm4 layui-col-md4 layui-btn-container">
            <button class="layui-btn layui-btn-radius layui-btn-primary" id="vip-btn-add" title="新增"><i class="vip-icon">&#xe65c;</i></button>
            <button class="layui-btn layui-btn-radius layui-btn-primary" id="vip-btn-del" title="删除"><i class="vip-icon">&#xe664;</i></button>
            <button class="layui-btn layui-btn-radius layui-btn-primary" id="vip-btn-refresh" title="刷新"><i class="vip-icon">&#xe68a;</i></button>
        </div>
        <div class="layui-col-sm12 layui-col-sm8 layui-col-md8">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-label-sm">标题</label>
                    <div class="layui-input-inline layui-input-sm">
                        <input type="text" name="search" required="" lay-verify="required" lay-vertype="tips" placeholder="请输入内容" autocomplete="off" class="layui-input" data-form-pw="1515934826270.1904">
                    </div>
                    <label class="layui-form-label layui-label-sm">选择</label>
                    <div class="layui-input-inline layui-input-sm">
                        <select name="select">
                            <option value=""></option>
                            <option value="1" selected="">选择一</option>
                            <option value="2">选择二</option>
                            <option value="3">选择三</option>
                            <option value="4">选择四</option>
                        </select>
                    </div>
                    <button id="vip-btn-search" type="button" class="layui-btn layui-btn-radius layui-btn-primary" title="搜索"><i class="vip-icon">&#xe6b0;</i></button>
                </div>
            </form>
        </div>
    </div>
</script>-->


<script type="text/javascript" src="../layui/layui.js"></script>
<script type="text/javascript">
layui.config({base: '../js/'}).use(['layer','table','form','app'],function(){
    layui.$.support.cors = true; //允许ajax跨域
    var $ = layui.$, layer = layui.layer, table = layui.table, form = layui.form, app = layui.app;

    // 表格
    var table1 = table.render({
        elem: '#demo-table-1'
        ,id: 'table-id-1'
        ,url: '../json/demo-table-1.json'   // 数据接口
        ,page: true                         // 开启分页
        ,loading: true                      // 开启loading
        ,cellMinWidth: 80                   // 每列最小宽度
        ,limits: [15,30,50]                 // 每页条数的选择项
        ,limit: 15                          // 默认每页条数
        ,cols: [[                           // 表头
            {field: 'id', title: 'ID', sort: true}
            ,{field: 'account', title: '用户名'}
            ,{field: 'auth_group_name', title: '权限组'}
            ,{field: 'credit', title: '信用'}
            ,{field: 'nickname', title: '昵称'}
            ,{field: 'status', title: '状态'}
            ,{title: '操作', fixed: 'right',width:160,align:'center', templet:'#menu'}
        ]]
    });

    // 表格
    var table2 = table.render({
        elem: '#demo-table-2'
        ,id: 'table-id-2'
        ,url: 'http://localhost:8088/questionanswer/page/1'   // 数据接口
        ,page: true                         // 开启分页
        ,loading: true                      // 开启loading
        ,cellMinWidth: 80                   // 每列最小宽度
        ,limits: [15,30,50]                 // 每页条数的选择项
        ,limit: 15                          // 默认每页条数
        ,toolbar: 'default'                 // 开启表格头部工具栏区域 该参数为 layui 2.4.0 开始新增。
        ,defaultToolbar: ['exports', 'filter', 'print'] // 自由配置头部工具栏右侧的图标 该参数为 layui 2.4.1 新增
        ,cols: [[                           // 表头
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', title: 'ID', sort: true}
            ,{field: 'title', title: '标题'}
            ,{field: 'titledescription', title: '标题描述'}
            ,{field: 'answer', title: '回答',totalRow: true}
            ,{field: 'createtime', title: '创建时间'}
            ,{field: 'lastupdatetime', title: '最近修改时间'}
            ,{field: 'createperson', title: '创建人'}
            ,{field: 'makerperson', title: '修改人'}
            ,{field: 'faqid', title: '问题ID'}
            ,{field: 'status', title: '状态'}
            ,{title: '操作', fixed: 'right',width:160,align:'center', templet:'#menu'}
        ]]
    });

    // 监听新增按钮
    $('#vip-btn-add').on('click',function(){
        app.msg('点击了新增按钮');
    });

    // 监听多选删除按钮
    $('#vip-btn-del').on('click',function(){
        var checkStatus = table.checkStatus('table-id-2'); // table-id-2 即为基础参数id对应的值

        if(!checkStatus.data.length){
            app.msg('未选中数据');
            return false;
        }

        if(checkStatus.isAll){
            app.msg('全选了:'+ app.getIds(checkStatus.data) );
        }else{
            app.msg( app.getIds(checkStatus.data) );
        }

        console.log(checkStatus.data);
        //console.log(checkStatus.data);        //获取选中行的数据
        //console.log(checkStatus.data.length); //获取选中行数量，可作为是否有选中行的条件
        //console.log(checkStatus.isAll );      //表格是否全选
    });

    // 监听表格刷新
    $('#vip-btn-refresh').on('click',function(){
        // 重载方式 1
        table2.reload();
        // 重载方式 2
        // table.reload('table-id-2', {})
    });

    // 监听搜索按钮
    $(document).on('click','#vip-btn-search',function(){
        var title = $('input[name=search]').val();
        var select = $('select[name=select]').val();
        if(!title){
            app.msg('搜索不能为空');
            return false;
        }

        layer.alert('标题：'+title+';选择：'+select);
    });

    //监听工具条
    table.on('tool(table)', function(obj){
        var data = obj.data;            //获得当前行数据
        var layEvent = obj.event;       //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr;                //获得当前行 tr 的DOM对象

        if(layEvent === 'view'){ // 查看

            app.msg('您点击了查看');
            // do somehing

        }else if(layEvent === 'edit'){ // 编辑

            app.msg('您点击了编辑');
            // do somehing

        } else if(layEvent === 'del'){ //删除

            layer.confirm('真的删除行么', function(index){
                obj.del(); // 删除对应行（tr）的DOM结构
                layer.close(index);
                // do somehing
            });

        }
    });
});
</script>
</body>
</html>